package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.Payment;
import ua.kharkov.knure.mikhaylov.st4.db.enums.AccountState;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - approove payment.
 * @author Saint
 *
 */
public class ApproovePaymentCommand extends Command {

	private static final long serialVersionUID = 5225798976893586481L;
	private static final Logger log = Logger
			.getLogger(ApproovePaymentCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		log.debug("Command starts");

		log.trace("Checking parameters");

		String forward = Path.PAGE_ERROR;
		String errorMessage = null;

		Long id = Long.valueOf(request.getParameter("id"));

		if (id == null) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.wrongparameters");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		Payment payment = DBManager.getInstance().findPaymentById(id);

		if (payment == null) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.notfound.payment");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		if (!payment.getPayer().getUser()
				.equals(request.getSession().getAttribute("user"))) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.permission.payment");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		if (payment.getPayer().getAccountState() == AccountState.CLOSED
				|| payment.getReciever().getAccountState() == AccountState.CLOSED) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.closedaccountpayment");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		if (payment.getPayer().getBalance() < payment.getVal()
				|| DBManager.getInstance().updatePaymentApproove(id) == false) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.notenoughmoney");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.payment.paid"));
		request.setAttribute("next", Path.COMMAND_VIEW_PAYMENTS);
		forward = Path.PAGE_SUCCESS;
		log.debug("Command finished");
		return forward;
	}

}
